
package com.instagram.instadownloader;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;

import com.instagram.instadownloader.fragment.BookMarkFragment;
import com.instagram.instadownloader.fragment.DownloadFragment;
import com.instagram.instadownloader.fragment.RecentDownloadFragment;
import com.instagram.instadownloader.utils.InstaFileManager;
import com.instagram.instadownloader.utils.Launcher;
import com.instagram.instadownloader.widget.PagerSlidingTabStrip;
import com.vine.vinedownloader.R;

public class MainActivity extends BaseInstaDownloaderActivity {

	private PagerSlidingTabStrip 	mSlidingTabStrip;
	private ViewPager 				mViewPager;
	private MyPagerAdapter 			mPagerAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
	}
	

	private void init(){
		try {
			FileUtils.forceMkdir(new File(InstaFileManager.INSTA_DOWNLOADER_FOLDER));
		} catch (IOException e) {
			e.printStackTrace();
		}
		mSlidingTabStrip = (PagerSlidingTabStrip) findViewById(R.id.tabs);
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());

		mViewPager.setAdapter(mPagerAdapter);

		final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
				.getDisplayMetrics());
		mViewPager.setPageMargin(pageMargin);

		mSlidingTabStrip.setViewPager(mViewPager);
		changeColor(getResources().getColor(R.color.hami_green));
		
		initListener();
	}
	
	public void initListener(){
		findViewById(R.id.actionbar_toggle).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(mSlidingMenu.isShown()){
					mSlidingMenu.showMenu();
				}else{
					mSlidingMenu.toggle();
				}
			}
		});
		
		findViewById(R.id.actionbar_instagram).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Launcher.launchInstagram(MainActivity.this);
			}
		});
		
		mSlidingTabStrip.setOnPageChangeListener(new OnPageChangeListener() {
			int currentPosition = 0;
			
			@Override
			public void onPageSelected(int newPosition) {
				Fragment fragmentToShow = mPagerAdapter.getItem(newPosition);
				if(fragmentToShow!=null && fragmentToShow instanceof FragmentLifecycle){
					((FragmentLifecycle)fragmentToShow).onResumeFragment();
				}

				Fragment fragmentToHide = mPagerAdapter.getItem(currentPosition);
				if(fragmentToHide!=null && fragmentToHide instanceof FragmentLifecycle){
					((FragmentLifecycle)fragmentToHide).onPauseFragment();
				}
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				
			}
		});
	}

	private void changeColor(int newColor) {

		mSlidingTabStrip.setIndicatorColor(newColor);
		mSlidingTabStrip.setTextColor(newColor);
	}

	public class MyPagerAdapter extends FragmentPagerAdapter {

		private final String[] TITLES = { "BOOKMARK", "DOWNLOAD", "RECENT DOWNLOAD"};
		private final Class<?>[] FragmentItemsClass = {BookMarkFragment.class,DownloadFragment.class,RecentDownloadFragment.class};
		public MyPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return TITLES[position];
		}

		@Override
		public int getCount() {
			return TITLES.length;
		}

		@Override
		public Fragment getItem(int position) {
			try {
				return (Fragment) FragmentItemsClass[position].newInstance();
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			} 
		}

	}
	
	public interface FragmentLifecycle {

		public void onPauseFragment();
		public void onResumeFragment();

	}

}